<?php 
    include("header.php");
    setTabIndex(1);
    createPage("Liga - Poll");
?>

<head>
    <title>Liga - Poll</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link href="assets/css/default.css" rel="stylesheet" type="text/css">

    <style type="text/css">
        @import "/assets/js/dojo-release-1.1.0/dijit/themes/tundra/tundra.css";
        @import "/assets/js/dojo-release-1.1.0/dojo/resources/dojo.css"
    </style>
    
    <script type="text/javascript" src="/assets/js/dojo-release-1.1.0/dojo/dojo.js"
      djConfig="parseOnLoad: true"></script>
    
    <script type="text/javascript">
        dojo.require("dijit.Dialog");
        dojo.require("dijit.form.TextBox");
        dojo.require("dijit.form.Button");
    </script>

</head>

<?php
    $user = $_SESSION['user'];
    
    mysql_connect(DB_SERVER, DB_USER, DB_PWD);
    mysql_select_db(DB_CATALOG);
    
    
    if (!isset($_POST['id'])) {
        if (!isset($_GET['id'])) {
            redirect("/newsbox.php");
        } else {
            $poll_id = $_GET['id'];
        }        
    } else {
        $poll_id = $_POST['id'];
    }
    
    $result = mysql_query("select p.* from poll p where p.poll_id = ".$poll_id);
    
    $polls = array();
    while ($row = mysql_fetch_assoc($result)) {
        $poll = new Poll;
        $poll->poll_id = $row['poll_id'];
        $poll->title = $row['title'];
        $poll->size = $row['size'];
        $poll->desc = $row['descr'];
        $poll->start = $row['start'];
        $poll->end = $row['end'];
        $poll->active = $row['active'];
        $poll->cht = $row['cht'];
        $poll->chxt = $row['chxt'];
        array_push($polls, $poll);
    }
    
    $activePoll = $polls[0];
    
    $result = mysql_query("select po.* from poll_option po where po.poll_id = ".$activePoll->poll_id);
    $pollOptions = array();
    $a = 0;
    while ($row = mysql_fetch_assoc($result)) {
        $a++;
        $option = new PollOption;
        $option->option_id = $row['poll_option_id'];
        $option->poll_id = $row['poll_id'];
        $option->label = $row['label'];
        $option->desc = $row['descr'];
        $option->color = $row['color'];
        $option->count = $row['count'];
        array_push($pollOptions, $option);
    }
    
    $result = mysql_query("select p.* from poll_comment p where p.poll_id = ".$poll_id);
    
    $pollComments = array();
    while ($row = mysql_fetch_assoc($result)) {
        $pollComment = new PollComment;
        $pollComment->poll_id = $row['poll_id'];
        $pollComment->choice_color = $row['choice_color'];
        $pollComment->comment = $row['comment'];
        array_push($pollComments, $pollComment);
    }

    $title = $activePoll->title;
    $enc_title = urlencode($activePoll->title);
    $chs = $activePoll->size;
    
    $total = 0;
    foreach ($pollOptions as $option) {
        $total += $option->count;
    }
    
    $b = 0;
    $chd = "t:";
    $chxr = "0,0,100";
    //$chtt = str_replace(" ", "+", $title);
    foreach ($pollOptions as $option) {
        if ($total > 0) {
            $chd = $chd.$option->count / $total;
        } else {
            $chd = $chd.$option->count;
        }
        $chdl = $chdl.str_replace(" ", "+", $option->desc);
        $chco = $chco.$option->color;
        $chm= $chm."N*p0*,000000,".$b.",-1,11";
        if (++$b < $a) {
            $chd = $chd."|";
            $chdl = $chdl."|";
            $chm = $chm."|";
            $chco = $chco.",";
        }
        
    }

?> 

<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#FFFFFF">

    <tr>
        <td id="leftFiller" valign="top" height="100%" rowspan="3" bgcolor="#666666">
            <table border="0" cellpadding="0" cellspacing="0" height="100%" width="8px">
                <tr><td>&nbsp;</td></tr>
            </table>
        </td>
        <td id="header" valign="top" width="100%">
            <table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
                <tr>
                    <td bgcolor="#666666" width="70%">
                        <?php include("inc_title.html"); ?>
                    </td>
                    <td bgcolor="#666666" align="right" valign="top">&nbsp;

                    </td>
                </tr>
            </table>
            <table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
                <tr>
                    <td>&nbsp;</td>
                    <?php include("inc_sublinks.html"); ?>   
                </tr>
                <tr><td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img src="/assets/images/spacer.gif" alt="" height="1" width="1"></td></tr>
                <tr>
                    <td>&nbsp;</td>
                    <td valign="top">
                        <table align="left" cellspacing="10" width="100%">
                            <tr>
                                <td colspan="4">
                                    <strong><?=$title?></strong>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <img src=http://chart.apis.google.com/chart?cht=<?=$activePoll->cht?>&chs=<?=$chs?>&chd=<?=$chd?>&chxt=<?=$activePoll->chxt?>&chdl=<?=$chdl?>&chco=<?=$chco?>&chm=<?=$chm?>&chds=0,1 />
                                    <p/>
                                    (totaal aantal stemmen : <?=$total?>)
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <table width="95%" cellspacing="0" cellpadding="2" border="0">
                                        <tr>
                                            <td colspan=2><strong>Feedback</strong></td>
                                        </tr>
                                        <?
                                        foreach ($pollComments as $item) {
                                        ?>
                                        <tr>
                                            <td width="20px" style="border-bottom:1px dashed #999999;background-color:#<?=substr($item->choice_color, 0, 6)?>">&nbsp;</td>
                                            <td style="border-bottom:1px dashed #999999;"><?=$item->comment?></td>
                                        </tr>
                                        <?
                                        }
                                        ?>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

<?php include("footer.php") ?>